#
# @lc app=leetcode.cn id=98 lang=python3
#
# [98] 验证二叉搜索树
#
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
# @lc code=start
# Definition for a binary tree node.

class Solution:
    max_val = -float("inf")
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        if root is None:
            return True
        flag = True
        flag = self.isValidBST(root.left)
        if not flag:
            return False
        if self.max_val >= root.val:
            return False
        self.max_val = root.val
        return self.isValidBST(root.right)

# @lc code=end

